<?php
function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

function checkUsername($username)
{
    if (!strlen($username)) {
        return 'username can not be empty';
    }
    return true;
}

function checkEmail($email)
{
    if (strlen($email) > 50) {
        return 'Length of email is mistake';
    } elseif (!preg_match('/^[a-z0-9]+@([a-z0-9]+\.)+[a-z]{2,4}$/i', $email)) {
        return 'Format of email is mistake';
    }
    return true;
}

function checkPassword($password)
{
    $pattern = '/^(?=.*[!^&])[A-Z](.{4,})\d$/';
//     (?=.*[!^&]) a forward assertion that must have 1 char in (!,^,&)
//     [A-Z] start with capital
//     (.{4,}) 4 or more character, plus start and end to be 6 more.
//     \d$ ending with digit
    if (!preg_match($pattern, $password)) {
        return 'Format of password is mistake.
 The password must:
 start with a capital alphabet,
 must have at least 6 characters ,
 one of these characters must be either !, ^, & and
 must end with a number.';
    }
    return true;
}

function Redirect($url, $permanent = false)
{
    if (headers_sent() === false)
    {
        header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
    }

    exit();
}


?>